package com.example.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * flow配置类
 *
 * @author ding
 * @since 2024/7/9
 */
//@Configuration
//@PropertySource("classpath:application-dev.yml")
//@Slf4j
//@EnableTransactionManagement
//public class FlowableConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration> {
//
//    //读取配置
//    @Value("${flow.username}")
//    private String user;
//    @Value("${flow.password}")
//    String password;
//    @Value("${flow.url}")
//    String jdbcUrl;
//    @Value("${flow.driver-class-name}")
//    String driverClass;
//    @Value("${flow.initialPoolSize}")
//    Integer initialPoolSize;
//    @Value("${flow.maxPoolSize}")
//    int maxPoolSize;
//
//    //配置数据源
//    @Bean
//    public DruidDataSource flowDataSource() throws PropertyVetoException {
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUsername(user);
//        dataSource.setPassword(password);
//        dataSource.setUrl(jdbcUrl);
//        dataSource.setDriverClassName(driverClass);
//        dataSource.setInitialSize(initialPoolSize);
//        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolSize);
//        return dataSource;
//    }
//
//    /**
//     * @return
//     * @throws PropertyVetoException
//     */
//    @Bean
//    @Primary
//    @ConfigurationProperties(prefix = "spring.datasource.druid")
//    public DruidDataSource dataSource() throws PropertyVetoException {
//        return new DruidDataSource();
//    }
//
//    //配置第一个数据源的事务管理器，定义bean名称为 transactionManagerOne
//    @Primary
//    @Bean(name = "transactionManagerOne")
//    public PlatformTransactionManager transactionManagerOne(@Qualifier("dataSource") DataSource dataSourceOne) {
//        return new DataSourceTransactionManager(dataSourceOne);
//    }
//
//    //flowable 事物管理器
//    @Bean(name = "transactionManagerTwo")
//    public PlatformTransactionManager transactionManagerTwo() throws PropertyVetoException {
//        return new DataSourceTransactionManager(flowDataSource());
//    }
//
//    @Override
//    public void configure(SpringAppEngineConfiguration engineConfiguration) {
//        try {
//            //把数据源设置进来
//            engineConfiguration.setDataSource(flowDataSource());
//            //配置对应的事务管理器
//            engineConfiguration.setTransactionManager(transactionManagerTwo());
//            log.info("配置flowable数据源成功");
//        } catch (PropertyVetoException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}
